﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

namespace OtechWebServer
{
    public partial class GetNoPsgConfig : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            string phoneNumber = "";
            if (Request.QueryString["PhoneNumber"] != null)
            {
                phoneNumber = Request.QueryString["PhoneNumber"];

                StringBuilder strSql = new StringBuilder();
                strSql.Append("select * from ttaxinopsgdistanceconfig ");
                strSql.Append("where PhoneNumber='");
                strSql.Append(phoneNumber);
                strSql.Append("'");

                MySqlConnection connection = new MySqlConnection();
                connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbconn"].ToString();
                using (MySqlCommand cmd = new MySqlCommand(strSql.ToString(), connection))
                {
                    try
                    {
                        DataSet ds = new DataSet();
                        
                        connection.Open();
                        MySql.Data.MySqlClient.MySqlDataAdapter da =
                            new MySql.Data.MySqlClient.MySqlDataAdapter(strSql.ToString(), connection);
                        da.Fill(ds);

                     

                        if (ds.Tables.Count != 0 && ds.Tables[0].Rows.Count != 0)
                        {
                            DateTime stime1 = DateTime.Parse(ds.Tables[0].Rows[0]["StartTime1"].ToString());
                            DateTime etime1 = DateTime.Parse(ds.Tables[0].Rows[0]["EndTime1"].ToString());
                            DateTime stime2 = DateTime.Parse(ds.Tables[0].Rows[0]["StartTime2"].ToString());
                            DateTime etime2 = DateTime.Parse(ds.Tables[0].Rows[0]["EndTime2"].ToString());
                            int distance1 = int.Parse(ds.Tables[0].Rows[0]["Distance1"].ToString());
                            int distance2 = int.Parse(ds.Tables[0].Rows[0]["Distance2"].ToString());

                            int nowdistance = 0;

                            if (etime1.TimeOfDay < stime1.TimeOfDay)
                            {
                                DateTime dt1 = DateTime.Parse(DateTime.Now.Date + " " + stime1.TimeOfDay);
                                DateTime dt2 = DateTime.Parse(DateTime.Now.AddDays(1).Date + " " + etime1.TimeOfDay);
                                if (DateTime.Now > dt1 && DateTime.Now <= dt2)
                                {
                                    nowdistance = distance1;
                                }
                                else
                                {
                                    nowdistance = distance2;
                                }
                            }
                            else
                            {
                                if (DateTime.Now.TimeOfDay > stime1.TimeOfDay
                              && DateTime.Now.TimeOfDay <= etime1.TimeOfDay)
                                {
                                    nowdistance = distance1;
                                }
                                else
                                {
                                    nowdistance = distance2;
                                }
                            }
                            //if (DateTime.Now.TimeOfDay > stime1.TimeOfDay
                            //    && DateTime.Now.TimeOfDay <= etime1.TimeOfDay)
                            //{
                            //    nowdistance = distance1;
                            //}
                            //else
                            //{
                            //    nowdistance = distance2;
                            //}
                            //if (DateTime.Now.TimeOfDay > stime2.TimeOfDay &&
                            //    DateTime.Now.TimeOfDay <= etime2.TimeOfDay)
                            //{
                            //    nowdistance = distance2;
                            //}
                            Response.Write(nowdistance);
                            //若为0则表示该车配置了时间表  但是当前时间不属于配置的两个时间段内
                        }
                        else
                        {
                            Response.Write("-1");//该车在数据库中无配置记录                        
                        }
                    }
                    catch (MySql.Data.MySqlClient.MySqlException ex)
                    {
                        Response.Write("-1");
                    }
                    finally
                    {
                        connection.Close();
                    }
                }

            }
            else
            {
                Response.Write("-1");
            } 
        }
    }
}